#include "stdafx.h"

namespace cookery
{
	class Pasty
	{
	public:
		Pasty();

		void AddFilling(unsigned char FillingType);
		void PrintFilling();
		void RemoveFilling(unsigned char FillingType);
		bool TestFilling(unsigned char FillingType);

	private:
		void AddFilligTypes();
		
	private:
		unsigned char _pFilling;
		std::map<unsigned char,std::string> _pFillingStringsMap;
	};

	const unsigned char APPLE     = 0x01;
	const unsigned char PINEAPPLE = 0x02;
	const unsigned char POTATO    = 0x04;
}

cookery::Pasty::Pasty() : _pFilling(0)
{
	AddFilligTypes();	
}

void cookery::Pasty::AddFilling(unsigned char FillingType)
{
	_pFilling |= FillingType;
}

void cookery::Pasty::PrintFilling()
{
	std::map<unsigned char, std::string>::iterator FillingStringsMapIter    = _pFillingStringsMap.begin();
	std::map<unsigned char, std::string>::iterator FillingStringsMapEndIter = _pFillingStringsMap.end();

	std::string ResultString = "This pasty with";

	if (_pFilling)
	{	
		bool IsPrevWord = false;

		while (FillingStringsMapIter != FillingStringsMapEndIter)
		{
			if (TestFilling(FillingStringsMapIter->first))
			{
				if (IsPrevWord)
				{
					ResultString += ',';
				}
				ResultString += ' ';
				ResultString += FillingStringsMapIter->second;
				IsPrevWord = true;
			}
			else
			{
				IsPrevWord = false;
			}
			++FillingStringsMapIter;
		}
	}
	else
	{
		ResultString += "out filling";
	}

	ResultString += ".\n";
	std::cout << ResultString.c_str();
}

void cookery::Pasty::RemoveFilling(unsigned char FillingType)
{
	_pFilling &= _pFilling ^ FillingType;
}

bool cookery::Pasty::TestFilling(unsigned char FillingType)
{
	return _pFilling & FillingType;
}

void cookery::Pasty::AddFilligTypes()
{
	_pFillingStringsMap.insert(std::pair<unsigned char,std::string>(APPLE,    "apple"));
	_pFillingStringsMap.insert(std::pair<unsigned char,std::string>(PINEAPPLE,"pineapple"));
	_pFillingStringsMap.insert(std::pair<unsigned char,std::string>(POTATO,   "potato"));		
}

int _tmain(int argc, _TCHAR* argv[])
{
	cookery::Pasty pasty;
	pasty.AddFilling(cookery::APPLE | cookery::PINEAPPLE);
	pasty.PrintFilling();
	std::cout << "Is this pasty with pineapple? " << (pasty.TestFilling(cookery::APPLE) ? "Yes." : "No.") << std::endl;
	pasty.AddFilling(cookery::POTATO);	
	pasty.PrintFilling();
	pasty.RemoveFilling(cookery::APPLE | cookery::PINEAPPLE | cookery::POTATO);
	pasty.PrintFilling();

	system("pause");
	return 0;
}

//     This pasty with apple, pineapple.
//     Is this pasty with pineapple? Yes.
//     This pasty with apple, pineapple, potato.
//     This pasty without filling.